package com.ts.login.daoimpl;

import com.ts.constant.SysConst;
import com.ts.jdbc.SysDB;
import com.ts.login.bean.UserInfo;
import com.ts.login.dao.IndexDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xhc on 2017/4/5.
 */
@Repository
public class IndexDaoImpl implements IndexDao {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    /**
     * 获得系统菜单
     * @param userInfo 用户信息
     * @return
     */
    public List<Map<String, Object>> getMenus(UserInfo userInfo)throws Exception {

        String sUserCode=userInfo.getUserCode();
        StringBuilder sql=new StringBuilder();
        // 判断验证当前用户岗位中是否含有案件管理菜单，如果没有返回提示
        if (!sUserCode.equalsIgnoreCase(SysConst.SysUser))
        {
            sql.append(" Select count(*) from s_postright t");
            sql.append(" where exists (select 1 from s_userpost u ");
            sql.append("    where u.fpostcode=t.fpostcode and u.fusercode='"+sUserCode+"')");
            int iResultCount = SysDB.getIntValue(jdbcTemplate,sql.toString());
            if (iResultCount == 0)
            {
                throw new Exception("当前用户未设置权限，请联系管理员进行设置！");
            }
        }
        sql=new StringBuilder();
        sql.append(" select t.fcode,t.fname,t.fparent,t.fisdetail,t.flevel,t.fmodel,t.fimgpath");
        sql.append(" ,m.fmodel furl,m.fstartpara,m.fstarttype");
        sql.append(" from s_menu t");
        sql.append(" left join s_model m on t.fmodel=m.fcode");
        if (!sUserCode.equalsIgnoreCase(SysConst.SysUser)) {
            sql.append(" inner join (");
            sql.append("    select m.fCode from s_menu m");
            sql.append("    Left Join s_Model c on m.fModel=c.fCode");
            sql.append("    left join s_postright o on o.fmodelcode=c.fcode");
            sql.append("    left join s_postright p on p.fpostcode=o.fpostcode");
            sql.append("    Left Join s_UserPost s on p.fPostCode = s.fPostCode");
            sql.append("    INNER JOIN s_post po ON s.fpostcode=po.fcode AND po.fisstop=0");
            sql.append("      Where (s.fUserCode =:usercode)");
            sql.append("     ) v On v.fCode Like rtrim(t.fCode) || '%'");
        }else
        {
            sql.append(" where m.fisAdmin=1 ");
        }
        sql.append("    order by 1 ");
        HashMap<String,Object> params=new HashMap<String,Object>();
        params.put("usercode",sUserCode);
        return SysDB.getRows(jdbcTemplate,sql.toString(),params);
    }
}
